package io.github.lee.app.unsplash.ui.main.search

import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.chad.library.adapter.base.loadState.trailing.TrailingLoadStateAdapter
import com.gyf.immersionbar.ktx.fitsTitleBar
import com.gyf.immersionbar.ktx.immersionBar
import dagger.hilt.android.AndroidEntryPoint
import io.github.lee.app.unsplash.databinding.ToolbarMainSearchBinding
import io.github.lee.app.unsplash.ui.search.SearchActivity
import io.github.lee.app.unsplash.views.LoadStateAdapter
import io.github.lee.core.ui.BaseRefreshFragment

@AndroidEntryPoint
class MainSearchFragment : BaseRefreshFragment<MainSearchVM>() {
    private val toolbarVb by lazy {
        ToolbarMainSearchBinding.inflate(layoutInflater).also {
            it.tvSearchInput.setOnClickListener {
                SearchActivity.start(mContext)
            }
        }
    }

    companion object {
        fun newInstance(): MainSearchFragment = MainSearchFragment()
    }

    //===Desc:======================================================================================

    override fun onCreateViewModel(): MainSearchVM =
        viewModels<MainSearchVM>().value


    //===Desc:======================================================================================

    override fun onCreateToolbar(): Toolbar {
        return toolbarVb.toolbar
    }

    override fun onCreateLayoutManager(): RecyclerView.LayoutManager =
        StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            .also {
                it.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
            }

    override fun onCreateTrailingLoadAdapter(): TrailingLoadStateAdapter<*> =
        LoadStateAdapter()

    //===Desc:======================================================================================


    override fun onSetViewData() {
        super.onSetViewData()
        immersionBar {
            fitsTitleBar(toolbarFrameLayout)
        }

        addHeaderAdapter(DiscoverHeaderAdapter())
    }


}